import axios from 'axios';

const instance = axios.create({
  baseURL: 'http://localhost:5173', // Change this to your API base URL
  timeout: 1000,
});

// Request interceptor
instance.interceptors.request.use(
  config => {
    // Add token to headers if available
    const token = localStorage.getItem('token');
    if (token) {
      config.headers.Authorization = `Bearer ${token}`;
    }
    return config;
  },
  error => {
    return Promise.reject(error);
  }
);

// Response interceptor
instance.interceptors.response.use(
  response => {
    return response.data;
  },
  error => {
    if (error.response) {
      switch (error.response.status) {
        case 401:
          // Handle unauthorized error
          alert('Unauthorized, please log in again.');
          break;
        case 500:
          // Handle server error
          alert('Server error, please try again later.');
          break;
        default:
          alert('An error occurred.');
      }
    }
    return Promise.reject(error);
  }
);

export default instance; 